import initcy from './initcy'
import drawTopo from './drawTopo'
import getPoint from './getPort'
import tools from './tools'
import Config from './config'
import eventBus from '../eventBus'
import selectPort from './selectPort'
import drawLink from './drawLink'
export default {
  start () {
    // var el = document.getElementById('app')
    initcy.init()
    // this.cyadddata()
    this.setEvent()
    Config.bizx = tools.getCookie('bizx')
    drawTopo.draw(initcy.getcy())
    this.documentEvent()
  },
  documentEvent () {
    document.addEventListener('mousemove', function (e) {
      eventBus.bus.$emit('mouseMove', {event: e})
    })
  },
  setEvent () {
    var cy = initcy.getcy()
    var dom = cy.getElementById('rose')
    var that = this
    cy.on('tap', 'node', function (evt) {
      var nodes = this
      selectPort.selectEl(this)
    })

    cy.on('mouseover', 'node', function (evt) {
      var that = this 
      eventBus.bus.$emit('showHideNodeTip', {
        show: true,
        vm: that,
        x: evt.cyPosition.x,
        y: evt.cyPosition.y
      })
    })

    cy.on('mouseout', 'node', function (evt) {
      eventBus.bus.$emit('showHideNodeTip', {
        show: false
      })
    })

    cy.on('mouseover', 'edge', function (evt) {
      var that = this 
      eventBus.bus.$emit('showHideEdgesTip', {
        show: true,
        vm: that,
        x: evt.cyPosition.x,
        y: evt.cyPosition.y
      })
    })

    cy.on('mouseout', 'edge', function (evt) {
      eventBus.bus.$emit('showHideEdgesTip', {
        show: false
      })
    })

    cy.on('click', 'node', function (evt) {
      console.log('evt xxx')
      drawLink.find(this.data())
    })
  },
  cyadddata () {
    var cy = initcy.getcy()
    cy.add({
      group: 'edges',
      data: { source: 'aphid', target: 'grasshopper', label: '线1' },
      position: { x: 0, y: 0 }
    })
    setTimeout(function () {
      cy.viewport({
        pan: { x: 100, y: 100 }
      })
    }, 2000)
  }
}
